/**
 * 调整 webpack 配置
 */
const path = require('path');
function Resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: process.env.NODE_ENV === 'production' ? '././' : '/', //Vue Cli 4.0 设置相对路径不再是 './'
  configureWebpack: config => {
    return {
      resolve: {
        alias: {
          '@views': path.join(__dirname, 'src/views')
        }
      }
    }
  },
  chainWebpack: config => {
    //svg sprites
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule.oneOf('icon').test(/\.icon\.svg$/)
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon_[hash:base64:5]',
      }).end()
      .use('svgo-loader').loader('svgo-loader').options({
        plugins: [
          {removeAttrs: {attrs: 'path:fill'}},
          { removeXMLNS: true }
        ]
      })
      
    //普通svg图片
    svgRule.oneOf('img').test(/\.svg$/)
      .use('file-loader')
      .loader('file-loader').options({
        name: 'img/[name].[hash:8].[ext]'
      })

    //vue-loader
    config.module
    .rule('vue')
    .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        // 修改它的选项...
        options.transformAssetUrls = {
          video: ['src', 'poster'],
          source: 'src',
          img: 'src',
          image: ['xlink:href', 'href'],
          use: ['xlink:href', 'href'],
          'vc-svg-icon': 'src'
        }
        return options
      })
  },
  css: {
    requireModuleExtension: true, //只有 *.module.[ext] 结尾的文件才会被视作 CSS Modules 模块
    loaderOptions: {
      css: {
        modules: {
          localIdentName: '[local]_[hash:base64:5]'  //默认：[name]_[local]_[hash:base64:5]，详见：https://github.com/webpack-contrib/css-loader#localidentname
        }
      },
      scss: {
        additionalData: `@import "~@/assets/styles/variable.scss";`
      },
      less: {
        lessOptions:{
          // less 全局变量
          // globalVars: {
          //   'primary-color': '#3eaf7c',
          // },
          modifyVars: {
            'primary-color': '#7546C9',
            'link-color': '#1DA57A',
            'border-radius-base': '2px',
          },
          javascriptEnabled: true //详见：https://github.com/ant-design/ant-motion/issues/44#issuecomment-620033459
        }
      }
    }
  }
}